﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Data;
using DatabaseAccess;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Text;

namespace Liguo.Administration
{
    public partial class AddGoods : System.Web.UI.Page
    {
        static String str1 = "1";
        static String str2 = "0";

        //static String str2 = "asd";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                
                SqlIt c1 = new SqlIt();

                DataSet ds = new DataSet();
                ds = c1.SelectAll("GoodsType");
                DropDownList1.DataSource = ds;
                DropDownList1.DataTextField = "TypeName";
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataBind();
                    SqlIt c2 = new SqlIt();
                DataSet ds1 = new DataSet();
                ds1 = c2.SelectAll("SaleMethod");
                DropDownList2.DataSource = ds1;
                DropDownList2.DataTextField = "Method";
                DropDownList2.DataValueField = "ID";
                DropDownList2.DataBind();
            }


        }
         protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Length > 20 || TextBox4.Text.Length > 7 || TextBox9.Text.Length > 20 || TextBox10.Text.Length > 5 || TextBox11.Text.Length > 5)
            {
                Response.Write("<script>alert('输入出错')</script>");
                return;
            }
            if (TextBox1.Text.Length == 0 || TextBox4.Text.Length == 0 || TextBox9.Text.Length == 0 || TextBox10.Text.Length == 0 || TextBox11.Text.Length == 0)
            {
                Response.Write("<script>alert('请输入数据')</script>");
                return;
            }
            if (!Regex.IsMatch(TextBox4.Text, "^([0-9]{1,}[.][0-9]*)$"))
            {

                Response.Write("<script>alert('请输入浮点数字')</script>");
                return;
            }
           
            
            if (FUpload.PostedFile.FileName=="")
            {
                Response.Write("<script>alert('请加入图片')</script>");
                return;
            }
            if (Regex.IsMatch(TextBox11.Text, @"^-?\d+$") || Regex.IsMatch(TextBox11.Text, "^([0-9]{1,}[.][0-9]*)$"))
            {

                SqlIt c1 = new SqlIt();
                String Goodsname = TextBox1.Text;
                String localpath = FUpload.PostedFile.FileName;
                string serverPath = Server.MapPath("~/images/");
                String fileName = DateTime.Now.Ticks.ToString() + ".png";
                //FUpload.SaveAs(serverPath + fileName);
                System.Drawing.Image image = Bitmap.FromFile(localpath);
                image.Save(serverPath + fileName, System.Drawing.Imaging.ImageFormat.Png);


                String Tags = TextBox3.Text;
                String DPrice = TextBox4.Text;


                String PicturePath = fileName;
                String Introduction = TextBox8.Text;
                String Manufacturers = TextBox9.Text;
                String GuaranteePeriod = TextBox10.Text;
                String Weight = TextBox11.Text;
                Dictionary<string, string> myDic = new Dictionary<string, string>();

                myDic["Goodsname"] = Goodsname;
                myDic["Tags"] = Tags;
                myDic["Price"] = DPrice;
                myDic["Type"] = str1;

                myDic["SaleMethod"] = str2;
                myDic["PicturePath"] = PicturePath;
                myDic["Introduction"] = Introduction;
                myDic["Manufacturers"] = Manufacturers;
                myDic["GuaranteePeriod"] = GuaranteePeriod;
                myDic["Weight"] = Weight;
                c1.AddMess("GoodsInfo", myDic);
                Response.Write("<script type='text/javascript'>alert('成功！');</script>");

            }
            else
            {
                Response.Write("<script>alert('请输入整数或浮点数')</script>");
                return;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("GoodsType.aspx");
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //重定向
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           str1=DropDownList1.SelectedValue;

        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            str2 = DropDownList2.SelectedValue;
        }

       

   
    }
}
